package com.etool.commons.interceptor;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.etool.commons.sql.SearchCondition;
import com.etool.commons.utils.AnnontationUtils;
import com.etool.modules.auth.model.AuthWorker;
import com.etool.modules.auth.model.NoRequireLogin;
import com.etool.modules.auth.service.AuthWorkerService;
import com.etool.modules.auth.service.CurrentWorkerAware;
import com.etool.modules.auth.util.loginUtil;
import com.etool.modules.curd.service.CurdService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;

import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 登录拦截器
 * @author linjia
 *
 */
public class UserLoginInterceptor implements Interceptor {

	private static final long serialVersionUID = -1997592576433280983L;

	protected static final Log logger = LogFactory.getLog(UserLoginInterceptor.class);
	
	private AuthWorkerService authWorkerService;
	
	private CurdService curdService;

	public String intercept(ActionInvocation invocation) throws Exception {
		final ActionContext context = invocation.getInvocationContext();
		final ActionProxy proxy = invocation.getProxy();
		final Object action = invocation.getAction();
		final HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
		final HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
		final HttpSession session=request.getSession();
		
		String pinyin = (String)session.getAttribute("pinyin");
		
		//如果是本地调试的话，默认管理员登录
		if(pinyin==null && "127.0.0.1".equalsIgnoreCase(request.getRemoteAddr()))
		{
			List<Map<String, Object>> list=curdService.read("auth_worker", Collections.singletonMap("pinyin", (Object)"admin"), 0, 1,null);
			loginUtil.login(request, list.get(0));
			return invocation.invoke();
		}
		
		
		final NoRequireLogin noLogin=AnnontationUtils.getFromMethedOrType(NoRequireLogin.class, action, proxy.getMethod());
		
		if(noLogin!=null)
		{
			return invocation.invoke();
		}
		
		if(StringUtils.isEmpty(pinyin))
		{
			return "login";
		}
		
		
		//如果实现了CurrentWorkerAware接口且用户存在，自动注入当前用户authWorker
		if(action instanceof CurrentWorkerAware)
		{		
			List<AuthWorker> authWorker=authWorkerService.select(Collections.singletonList(new SearchCondition("pinyin", pinyin)));
			if(authWorker!=null && authWorker.size()==1)
			{
				((CurrentWorkerAware) action).setAuthWorker(authWorker.get(0));
			}
		}
		
		return invocation.invoke();
	}

	

	public void destroy() {
	
	}

	public void init() {
		
	}



	public AuthWorkerService getAuthWorkerService() {
		return authWorkerService;
	}



	public void setAuthWorkerService(AuthWorkerService authWorkerService) {
		this.authWorkerService = authWorkerService;
	}



	public CurdService getCurdService() {
		return curdService;
	}



	public void setCurdService(CurdService curdService) {
		this.curdService = curdService;
	}

	
}
